﻿using Inaction.Message;
using Inaction.Rule;
using Inaction.Service.Client.Proxy;
using System;
using System.Windows.Forms;

namespace Inaction.Service.Client
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            if (Inaction.AppConfig.Instance.ServiceType == ServiceType.Local)
            {
                Inaction.AppConfig.Instance.RunLocation = RunLocation.Local;
            }
            else
            {
                Inaction.AppConfig.Instance.RunLocation = RunLocation.Client;
            }
            MessageHost.ReceivedMessage += ShowMessage;
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageHost.ReceivedMessage -= ShowMessage;
        }

        void ShowMessage(object obj)
        {
            if (labMessageInfo.InvokeRequired)
            {
                Action<object> a = ShowMessage;
                this.Invoke(a, new object[] { obj });
            }
            else
            {
                labMessageInfo.Text = obj.ToString();
            }
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            try
            {
                var error = this.errorProvider.GetError(tb_Number) + errorProvider.GetError(tb_Password);
                if (!string.IsNullOrEmpty(error))
                {
                    ShowMessage(error);
                    return;
                }
                ShowMessage("正在登陆");
                Proxy.ProxyFactory.InitProxy();
                Inaction.Proxy.Login(tb_Number.Text, tb_Password.Text);
                if (Inaction.Security.Principal.Instance.Identity.IsAuthenticated)
                {
                    DialogResult = System.Windows.Forms.DialogResult.Yes;
                    if (Inaction.AppConfig.Instance.RunLocation != RunLocation.Local)
                    {
                        Inaction.ClientUpdator.Update();
                    }
                    Close();
                }
                else
                {
                    ShowMessage("登陆失败,请检查用户与密码是否一致!");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetMessage());
                Inaction.Util.Loger.WriteLog(ex);
            }
        }

        private void btn_Config_Click(object sender, EventArgs e)
        {
            new AppConfigForm().ShowDialog();
        }

        private void Text_Validated(object sender, EventArgs e)
        {
            var text = sender as TextBox;
            if (string.IsNullOrEmpty(text.Text))
            {
                errorProvider.SetError(text, "值不能为空");
            }
            else if (text.Text.Length < 3)
            {
                errorProvider.SetError(text, "字符长度为3~10位");
            }
            else
            {
                errorProvider.SetError(text, "");
            }

            ShowMessage(errorProvider.GetError(text));
        }

        public static bool Login(string userNumber, string password)
        {
            try
            {
                Proxy.ProxyFactory.InitProxy();
                Inaction.Proxy.Login(userNumber, password);
                if (Inaction.Security.Principal.Instance.Identity.IsAuthenticated)
                {
                    if (Inaction.AppConfig.Instance.RunLocation != RunLocation.Local)
                    {
                        Inaction.ClientUpdator.Update();
                    }
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetMessage());
                Inaction.Util.Loger.WriteLog(ex);
                return false;
            }
        }
    }
}
